//
//  MainTabBar.swift
//  传智小微博
//
//  Created by lxgoodgo on 16/4/17.
//  Copyright © 2016年 liux.orz. All rights reserved.
//

import UIKit

//声明一个 协议
protocol TabBarDelegate : NSObjectProtocol
{
    func composeButtonClick()
}


class MainTabBar: UITabBar {

    //声明一个代理
    weak var Delegate : TabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        self.tintColor = UIColor.orangeColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加 撰写 按钮
    func setUpUI()
    {
        addSubview(composeButton)
    }
    
    //private 有毛用
    //懒加载 撰写 按钮
    private lazy var composeButton : UIButton =
    {
        let button = UIButton()
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 设置button 背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted") , forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add") , forState: UIControlState.Normal)
        
        button.sizeToFit()
        return button

    }()
    // 点击 撰写按钮 监听事件
    @objc private func composeButtonClick()
    {
        print("entry composeButtonClick")
        Delegate?.composeButtonClick()
    }
    
    //对 子控件 布局
    
    override func layoutSubviews() {
        let width = self.frame.size.width/5
        
        var index: CGFloat = 0
        for value in self.subviews
        {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                value.frame.origin.x = index * width
                value.frame.size.width = width
                
            
                index++
                if(index == 2)
                {
                    index++
                }
            }
        }
        composeButton.center.x = self.frame.width*0.5
        composeButton.center.y = self.frame.height*0.5
        
        
    }
    
    
    
    
    
    
    
    
    
    
}
